package cn.web.web_module.rabbitmq.consumber;

import cn.itcast.mapper.horsemanMapper;
import cn.itcast.mapper.orderMapper;
import cn.itcast.pojo.horseman;
import cn.itcast.pojo.order;
import cn.web.web_module.entity.WithdrawRechargeInfo;
import cn.web.web_module.mapper.WithdrawRechargeInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 张松
 * @Description:
 * @Date: Created in 8:26 2021/3/31
 * @Modified By:
 */
@Slf4j
@Component
@RabbitListener(queues={"direct_RechargeInfo.queue"})
public class DirectRechargeInfoConsumber {

    @Resource
    private horsemanMapper horsemanMapper;

    @Resource
    private orderMapper orderMapper;

    @Resource
    private WithdrawRechargeInfoMapper withdrawRechargeInfoMapper;

    /**
     *
     * @Description: withdraw_recharge_info的消费者
     * 
     * @auther: 张松
     * @date: 11:25 2021/3/31
     * @param: [notifyUrlParameter]
     * @return: void
     *
     */
    @RabbitHandler
    public void directRechargeInfoMessage(Map<String,String> notifyUrlParameter){

        String openid = notifyUrlParameter.get("openid");
        List<horseman> horsemen = horsemanMapper.selectHorsemanOpenId(openid);
        List<order> orders = orderMapper.selectHorsemanStocksOrder(openid);
        Integer orderId=0;
        if (orders.size()>0||!orders.isEmpty()){
            orderId=orders.get(0).getId();
        }
        WithdrawRechargeInfo withdrawRechargeInfo = new WithdrawRechargeInfo();
        withdrawRechargeInfo.setRechargeMoney(Long.parseLong(notifyUrlParameter.get("total_fee"))/100);
        withdrawRechargeInfo.setRechargeNumber(notifyUrlParameter.get("out_trade_no"));
        withdrawRechargeInfo.setHorsemanName(horsemen.get(0).getHorsemanName());
        withdrawRechargeInfo.setHorsemanAccount(openid);
        withdrawRechargeInfo.setRechargeTime(new Date());
        withdrawRechargeInfo.setOrderId(orderId);
        int i = withdrawRechargeInfoMapper.insertSelective(withdrawRechargeInfo);
        log.info(horsemen.get(0).getHorsemanName()+":"+openid+"记录withdrawRechargeInfo成功"+"充值了:"+notifyUrlParameter.get("total_fee")+"元");
    }
    
    
    

}
